package com.springboot.log;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

@Component
public class ApplicationContextAop implements PointcutAdvisor {
    @Override
    public Pointcut getPointcut() {
        return new Pointcut() {
            @Override
            public MethodMatcher getMethodMatcher() {
                MethodMatcher matcher = new MethodMatcher() {
                    @Override
                    public boolean matches(Method method, Class<?> aClass) {
                        return match(method, aClass);
                    }

                    @Override
                    public boolean isRuntime() {
                        return true;
                    }

                    @Override
                    public boolean matches(Method method, Class<?> aClass, Object... objects) {
                        return match(method, aClass);
                    }

                    private boolean match(Method method, Class<?> aClass) {
//                        Arrays
//                        if (CollectionU(aClass.getInterfaces(), ApplicationContext.class) >= 0) {
//                            return method.getName().equals("publishEvent");
//                        }
                        return false;
                    }
                };
                return matcher;
            }

            @Override
            public ClassFilter getClassFilter() {
                return aClass -> true;
            }
        };
    }

    @Override
    public Advice getAdvice() {
        return new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                return invocation.proceed();
            }
        };
    }

    @Override
    public boolean isPerInstance() {
        return true;
    }
}
